package pATT.profilingAlgorithm.PersonalSearcher;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;

import pATT.logger.Message;



public class StopWords implements DocumentFilter {
	
	private HashSet	stopwords = new HashSet();
	private String 	language; 
	
	@SuppressWarnings("unchecked")
	public StopWords(String lang){
		language=lang;
		BufferedReader in;
		try{
			
//			in= new BufferedReader(new FileReader(Settings.getDataPath() + language.toLowerCase() + ".stopwords"));
			String dir = Message.PATH_PERSONALSEARCHER_DATA;

//			String dir = "C:/Documents and Settings/Ignacio/Escritorio/Eclipse/PersonalSearcher/data/";
			in= new BufferedReader(new FileReader(dir + language.toLowerCase() + ".stopwords"));
			
			String strWord;
			while((strWord=in.readLine())!= null){
				stopwords.add(strWord);
			}
			in.close();
			
		}catch(IOException e){
			System.out.println("Can not get the stopwords");
			System.exit(-1);
		}
	}
	
	public Instance applyFilter(Instance i) {
		TermVector v = i.getVector();
		TermVector newv = new TermVector();
		
		for (Iterator it=v.termIterator(); it.hasNext(); ) {
			String s = it.next().toString();
			if (!stopwords.contains(s)) {
				newv.put(s,v.get(s));
			}
		}
		i.setVector(newv);
		return i;
	}
	
	@SuppressWarnings("unchecked")
	public void addStopWord(String strWord) {
		stopwords.add(strWord);
	}
	
	public boolean contains(String strWord) {
		return stopwords.contains(strWord);
	}
	
	public int size(){
		return stopwords.size();
	}
	
	public void removeStopWord(String strWord){
		stopwords.remove(strWord);	
	}	
	
	public void removeAllStopWords(){
		stopwords.clear();
	}
	
	public HashSet getAllStopWords(){
		return stopwords;
	}
}
